import { ExportExcel } from "@/utils/excel/exportExcel";

export default {
  data() {
    return {
      tableHeads: [],
      w: [{ wpx: 180 }, { wpx: 180 }, { wpx: 180 }, { wpx: 180 }, { wpx: 180 }, { wpx: 180 }, { wpx: 180 }]
    };
  },
  methods: {
    download() {
      const excel = new ExportExcel();
      const headerLList = this.exportSetHeaderList(this.searchForm.form.reportType, this.tableHead);

      // 需要合并的规则
      const merges = this.exportGetMerges();
      console.log(headerLList, "headerLList");
      excel.exportData(this.dataGrid.data, headerLList, this.searchForm.form.reportType, this.exportStyleCell, this.w, merges, this.exportDel);
    },
    exportSetHeaderList(reportType, tableHead) {
      const headerList = [
        {
          label: `${reportType}`,
          prop: "a",
          childern: [
            {
              label: "",
              prop: "",
              childern: [
                {
                  label: "日期",
                  prop: "date"
                },
                {
                  label: "客户",
                  prop: "userName"
                },
                {
                  label: "品名",
                  prop: "name"
                },
                {
                  label: "素材类型",
                  prop: "incomingModel"
                }
              ]
            }
          ]
        },
        {
          label: "总数",
          prop: "",
          childern: [
            {
              label: "",
              prop: "",
              childern: [
                {
                  label: "型号",
                  prop: "Model"
                }
              ]
            }
          ]
        },
        {
          label: tableHead.amount || "0",
          prop: "",
          childern: [
            {
              label: "",
              prop: "",
              childern: [
                {
                  label: "投入数",
                  prop: "totalAmount"
                }
              ]
            }
          ]
        },
        {
          label: tableHead.goodAmount || "0",
          prop: "",
          childern: [
            {
              label: "",
              prop: "",
              childern: [
                {
                  label: "良品数",
                  prop: "goodAmount"
                }
              ]
            }
          ]
        },
        {
          label: tableHead.goodRate || "0",
          prop: "",
          childern: [
            {
              label: "",
              prop: "",
              childern: [
                {
                  label: "良率",
                  prop: "goodRate"
                }
              ]
            }
          ]
        },
        {
          label: tableHead.ngRate || "0",
          prop: "",
          childern: [
            {
              label: "",
              prop: "",
              childern: [
                {
                  label: "不良率",
                  prop: "ngRate"
                }
              ]
            }
          ]
        },
        {
          label: "责任划分",
          prop: "",
          childern: [
            {
              label: "不良率",
              prop: "",
              childern: [
                {
                  label: tableHead.ngTotalAmount || "0",
                  prop: "",
                  childern: [
                    {
                      label: "不良总数",
                      prop: "ngTotalAmount"

                    }
                  ]
                }
              ]
            }
          ]
        }

      ];
      this.tableHeads = tableHead;
      const dynamicHead = this.exportGetDynamicHead(this.ngList, this.userNgList);
      headerList.push(...dynamicHead);

      return headerList;
    },
    // 动态的表格头部信息
    exportGetDynamicHead(arr1, arr2) {
      const newArr = [];
      const obj1 = {
        label: "",
        prop: "",
        childern: []
      };
      const obj2 = {
        label: "客户责任",
        prop: "",
        childern: []
      };
      arr1.forEach((item) => {
        const _obj = {
          label: item.unqualifiedRateObj,
          prop: "",
          childern: [
            {
              label: item.amountObj + "",
              prop: "",
              childern: [
                {
                  label: item.name,
                  prop: item.id + "-"

                }
              ]
            }
          ]
        };
        obj1.childern.push(_obj);
      });
      arr2.forEach((item) => {
        const _obj2 = {
          label: item.unqualifiedRateObj,
          prop: "",
          childern: [
            {
              label: item.amountObj + "",
              prop: "",
              childern: [
                {
                  label: item.label,
                  prop: item.id + "-"
                }
              ]
            }
          ]
        };
        obj2.childern.push(_obj2);
      });
      if (arr1.length) {
        newArr.push(obj1);
      }
      if (arr2.length) {
        newArr.push(obj2);
      }
      return newArr;
    },
    // 当嵌套规则无法满足条件时设置   新增的表格合并规则
    exportGetMerges() {
    // 需要添加的合并规则
      const merges = [
        {
          "s": {
            "r": 0,
            "c": 0
          },
          "e": {
            "r": 1,
            "c": 3
          }
        }
      ];
      for (let i = 0; i < 6; i++) {
        const obj = {
          "s": {
            "r": 0,
            "c": 4 + i
          },
          "e": {
            "r": 1,
            "c": 4 + i
          }
        };
        merges.push(obj);
      }

      return merges;
    },
    /**
   * @description: 需要删除的排序规则 如果需要修改合并规格  那么需要把原有的合并规则删除在添加新的合并规则   不然旧版的excel  会报错合并规则错误，导致合并规则错乱
   * @param {Array} merges 原有的合并规则
  */

    exportDel(merges) {
    // s.r  -> e.r  从 s.r列 合并到 e.r
    // s.c  -> e.c  从 s.c行 合并到 e.c

      // 从原有的合并规则中找出来需要删除的合并规则 (不可直接使用下标删除 原合并中的规则会根据数据的变化而变化位置，，导致下标删除规则异常)
      const delMerges = [
        {
          "s": {
            "r": 0,
            "c": 0
          },
          "e": {
            "r": 0,
            "c": 3
          }
        },
        {
          "s": {
            "r": 1,
            "c": 0
          },
          "e": {
            "r": 1,
            "c": 3
          }
        }
      ];
      // 新的合并规则
      const newMerges = [];

      for (let i = 0; i < merges.length; i++) {
        let count = 0;
        const length = delMerges.length - 1;

        for (let j = 0; j < delMerges.length; j++) {
        // 找到对应的合并规则
          if (JSON.stringify(merges[i]) === JSON.stringify(delMerges[j])) {
            count++;
          }
          if (j === length && !count) {
            newMerges.push(merges[i]);
          }
        }
      }
      return newMerges;
    },
    /**
   * @description: 表头样式设置
   * @param R 行的下标
   * @param C 列的下标
   * @param cellR 样式集合
  */
    exportStyleCell(R, C, cell) {
      const length = this.ngList.length + 10;

      // 生产不良品头部样式
      if ((C > 8 && C < length && (R === 0 || R === 1 || R === 3)) || C === 7) {
        return {
          fill: {
            patternType: "solid",
            fgColor: { theme: 3, tint: 0.3999755851924192, rgb: "FDF6EC" },
            bgColor: { theme: 7, tint: 0.3999755851924192, rgb: "FDF6EC" }
          },
          font: {
            name: "宋体",
            sz: 12,
            color: { rgb: "ff0000" },
            bold: true,
            italic: false,
            underline: false
          }
        };
      }
      return "";
    }
  }
};
